﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EzDesk.DesktopClient.ViewModel
{
    using EzDesk.DesktopClient.ViewContract;
    using EzDesk.MVVMBase;
    using EzDesk.DesktopClient.Model;
    using EzDesk.EventAggregation;
    using EzDesk.DesktopClient.Event;
    using EzDesk.Generic;
    using EzDesk.DesktopClient.MVVMEnhance;
    public class LoginViewModel : ViewModelWithSingleContainer<ILoginView>
    {

        #region Fields
        private string _userName = string.Empty;
        private string _password = string.Empty;
        private LoginEvent _loginEvent = EventAggregator<LoginEvent>.GetEvent();
        private UserManagement _userManagement;
        private bool _isLoging = false;
        private string _errorMessage;
        #endregion Fields
        #region Properties


        public string UserName
        {
            get { return _userName; }
            set
            {
                _userName = value;
                RaisePropertyChanged("UserName");
            }
        }
        public string Password
        {
            get { return _password; }
            set
            {
                _password = value;
                RaisePropertyChanged("Password");
            }
        }
        public bool IsEnable
        {
            get
            {
                return !_isLoging;
            }
            set
            {
                _isLoging = !value;
                RaisePropertyChanged("IsEnable");
            }
        }

        public string PasswordLink
        {
            get
            {
                return "http://www.g.cn";
            }
        }

        public string ErrorMessage
        {
            get { return _errorMessage; }
            set
            {
                _errorMessage = value;
                RaisePropertyChanged("ErrorMessage");
            }
        }

        #endregion Properties
        #region Constructor
        public LoginViewModel():base()
        {
            InitField();
            InitEvent();
        }
        private void InitField()
        {
            _userManagement = UserManagement.Instance;
        }
        private void InitEvent()
        {
            InitGlobalEvent();
            InitViewEvent();
        }
        private void InitGlobalEvent()
        {
            _loginEvent.Subscribe(this.LoginFinished);
        }
        private void InitViewEvent()
        {
            this.View.OnLogin += new EventHandler<GenericEventArg<string>>(this.Login);
        }
        #endregion Constructor

        void Login(object sender, GenericEventArg<string> e)
        {
            IsEnable = false;
            Password = e.Value;
            _userManagement.Login(UserName, Password);

        }

        void LoginFinished(LoginArgs loginArgs)
        {
            IsEnable = true;
            ErrorMessage = loginArgs.Message;
            loginArgs.IsSuccess.FalseDo(this.View.LoginFailed);
        }
    }
}
